package com.gopay.bis.icbc.ebank.operation;

import java.io.FileNotFoundException;

import cn.com.infosec.icbc.ReturnValue;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.TransUtils;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.icbc.ebank.pay.IcbcB2bMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.ICertHelperIcbc;

/**
 * 工行B2B签名验签实现
 * 
 * @IcbcB2bCertHelper.java
 * @author WangHongFei
 */
public class IcbcB2bCertHelper implements ICertHelperIcbc {

	public String sign(BaseMessage message) throws GopayException{
		try {
			IcbcB2bMessage messageb2b = (IcbcB2bMessage) message;
			BankLogUtils.printB2BSignPlainLog(messageb2b);
			byte[] transDataByte = messageb2b.getPlainMessage().getBytes("UTF-8");
			byte[] fileSkByte = null;
			char[] keyStorePwdChar = null;
			fileSkByte = TransUtils.getFile(messageb2b.getPrivateKeyPath());
			keyStorePwdChar = messageb2b.getPrivatePwd().toCharArray();
			byte[] transDataSignature = null;
			transDataSignature = ReturnValue.sign(transDataByte,transDataByte.length, fileSkByte, keyStorePwdChar);
			byte[] encTransDataSignature = ReturnValue.base64enc(transDataSignature);
			String signatureWithBase64 = new String(encTransDataSignature);
			BankLogUtils.printB2BSignSignatureLog(messageb2b, signatureWithBase64);
			return signatureWithBase64;
		} catch (Exception e) {
			BankLogUtils.printB2BSignExceptionLog(message, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6028);
		}
	}

	public Object verify(BaseMessage message) throws GopayException{
		try {
			IcbcB2bMessage b2bMessage = (IcbcB2bMessage) message;
			byte[] plainSrc = b2bMessage.getPlainMessage().getBytes("GBK");
			byte[] signSrc = b2bMessage.getSignMessage().getBytes("GBK");
			byte[] verifyPkByte = null;
			verifyPkByte = TransUtils.getFile(b2bMessage.getPrivateKeyPath2nd());
			signSrc = ReturnValue.base64dec(signSrc);
			int verifyRes = ReturnValue.verifySign(plainSrc,plainSrc.length, verifyPkByte, signSrc);
			BankLogUtils.printB2BVerifyPlainLog(b2bMessage);
			BankLogUtils.printB2BVerifySignatureLog(b2bMessage);
			BankLogUtils.printB2BVerifyResultLog(b2bMessage, verifyRes==0);
			return verifyRes == 0;
		} catch (Exception e) {
			BankLogUtils.printB2BVerifyExceptionLog(message, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6030);
		}
	}
	
	public String getPk(String pkFilePath) throws GopayException{
		BaseMessage baseMessage = new BaseMessage();
		baseMessage.setOutStlmCd(BankConstants.BANK_LOGO_ICBC); // 为了区分日志输出
		try {
			byte[] filePkByte = null;
			byte[] encPkByte = null;
			filePkByte = TransUtils.getFile(pkFilePath);
			encPkByte = ReturnValue.base64enc(filePkByte);
			return new String(encPkByte);
		} catch (FileNotFoundException e) {
			BankLogUtils.printB2BSignExceptionLog(baseMessage, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6028);
		}
		
	}

}
